package com.zhoug.http3;

import android.graphics.Bitmap;
import android.text.TextUtils;

import com.zhoug.logger3.Logger;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 请求参数工具
 *
 * @Author HK-LJJ
 * @Date 2019/10/24
 * @Description TODO
 */
public class RequestBodyUtils {
    private static final String TAG = ">>>RequestBodyUtils";
    /**
     * @see ContentType#OTCET_STREAM
     */
    @Deprecated
    public static final String TYPE_FILE = ContentType.OTCET_STREAM;
    /**
     * @see ContentType#JSON
     */
    @Deprecated
    public static final String TYPE_JSON = ContentType.JSON;


    /**
     * @param map 请求参数
     * @return Map<String, RequestBody>
     */
    public static Map<String, RequestBody> partMap(Map<String, String> map) {
        if (map != null && !map.isEmpty()) {
            Map<String, RequestBody> partMap = new HashMap<>();
            //将String封装成RequestBody类型
            for (Map.Entry<String, String> entry : map.entrySet()) {
                partMap.put(entry.getKey(), RequestBody.create(MediaType.parse(ContentType.JSON), entry.getValue()));
            }
            return partMap;
        }

        return null;
    }

    /**
     * @param map 请求参数
     * @return List<MultipartBody.Part>
     */
    public static List<MultipartBody.Part> partList(Map<String, String> map) {
        if (map != null && !map.isEmpty()) {
            List<MultipartBody.Part> partList = new ArrayList<>();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                MultipartBody.Part part = MultipartBody.Part.createFormData(entry.getKey(), entry.getValue());
                partList.add(part);
            }
            return partList;
        }

        return null;
    }


    /**
     * @param paths 上传的文件路径集合
     * @return List<MultipartBody.Part>
     */
    public static List<MultipartBody.Part> partFileList(List<String> paths) {
        if (paths != null && !paths.isEmpty()) {
            List<MultipartBody.Part> partList = new ArrayList<>();
            for (int i = 0; i < paths.size(); i++) {
                String path = paths.get(i);
                if (TextUtils.isEmpty(path)) {
                    Logger.e(TAG, "paths集合中的第" + i + "个path为空");
                }
                //将文件路径封装成MultipartBody.Part类型
                File file = new File(path);
                RequestBody requestBody = RequestBody.create(MediaType.parse(ContentType.OTCET_STREAM), file);
                MultipartBody.Part part = MultipartBody.Part.createFormData("file" + i, file.getName(), requestBody);
                partList.add(part);
            }
            return partList;
        }

        return null;
    }

    /**
     * @param bitmaps 上传的图片Bitmap
     * @param bitmapMaxSize 单位KB,上传Bitmap时压缩到的最大字节大小 d大于0时压缩
     * @return List<MultipartBody.Part>
     */
    public static List<MultipartBody.Part> partBitmapList(List<Bitmap> bitmaps,int bitmapMaxSize) {
        if (bitmaps != null && bitmaps.size() > 0) {
            List<MultipartBody.Part> partList = new ArrayList<>();
            long currentTimeMillis = 0;
            for (int i = 0; i < bitmaps.size(); i++) {
                Bitmap bitmap = bitmaps.get(i);
                //将文件路径封装成MultipartBody.Part类型
                byte[] bytes;
                if (bitmapMaxSize > 0) {
                    //压缩
                    bytes = Utils.compressQualityTo(bitmap, bitmapMaxSize);
                } else {
                    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, arrayOutputStream);
                    bytes = arrayOutputStream.toByteArray();
                }
                RequestBody requestBody = RequestBody.create(MediaType.parse(ContentType.OTCET_STREAM), bytes, 0, bytes.length);
                long l = System.currentTimeMillis();
                if (currentTimeMillis == 0 || l != currentTimeMillis) {
                    currentTimeMillis = l;
                } else {
                    currentTimeMillis += 1;
                }
                MultipartBody.Part part = MultipartBody.Part.createFormData("file" + i, currentTimeMillis + ".png", requestBody);
                partList.add(part);
            }
            return partList;
        }

        return null;
    }


}
